﻿using Inovout.Cloud.Applications.Models;
using Inovout.Cloud.Applications.Security.Models;
using Inovout.Memberships;
using Inovout.Security;
using System.Collections.Generic;
using System.Security.Claims;

namespace Inovout.Cloud.Applications.Security
{
    public class ApplicationUserPrincipal : ClaimsPrincipal
    {
        public Application Application { get { return ((ApplicationUser)this.ApplicationUser).Application; } }
        public IApplicationUser ApplicationUser { get; private set; }

        public IMembershipUser MembershipUser { get; private set; }
        public ApplicationUserPrincipal(IApplicationUser applicationUser, IMembershipUser membershipUser)
            : base(CreateClaimsIdentity(applicationUser, membershipUser))
        {
            this.MembershipUser = membershipUser;
            this.ApplicationUser = applicationUser;
        }
        public const string DefaultApplicationNameClaimType = "http://schemas.inovout.com/identity/claims/application_name";
        private static ClaimsIdentity CreateClaimsIdentity(IApplicationUser applicationUser, IMembershipUser membershipUser)
        {
            var claims = new List<Claim>();
            claims.Add(new Claim(ClaimsIdentity.DefaultNameClaimType, membershipUser.IdentifierCode));
            claims.Add(new Claim(DefaultApplicationNameClaimType, applicationUser.Name));
            return new ClaimsIdentity(claims, "Inovout Application User Principal");

        }

    }
}
